package cn.lang.manager;

import cn.hutool.core.util.StrUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @author Lang 1102076808@qq.com
 * @description 邮件服务
 * @date 2020-10-30 22:23
 */
@Component
public class MailManager {

	@Autowired
	private JavaMailSender mailSender;

	@Value("${mail.from}")
	private String from;

	/**
	 * @description 发邮件
	 * @author Lang
	 * @date 2020/10/30 22:29
	 */
	public void sendMail(String subject, String content,String ...to) {
		MimeMessage message = mailSender.createMimeMessage();
		MimeMessageHelper messageHelper = new MimeMessageHelper(message);
		try {
			messageHelper.setFrom(from);
			messageHelper.setTo(to);
			messageHelper.setSubject(subject);
			messageHelper.setText(content, true);
		} catch (MessagingException e) {
			e.printStackTrace();
		}
		mailSender.send(message);
	}

	public void sendMail(String date,String holidayName){
		String content = "<h1 style=\"text-align: center;\">小张的节日提醒来啦</h1>\n" +
				"\t\t<p style=\"margin-top:20px\">{}是 <span style=\"font-weight: bold;color:#f44336;\">{}</span> ！！！ " +
				"记得和爱你的人和你爱的人说声节日快乐哟！</p>";
		sendMail("噔噔噔！！！小张的节日提醒", StrUtil.format(content,date,holidayName),"1102076808@qq.com","2018352975@qq.com");
	}
}
